//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //服务器图片的路径片段
    imgUrl:app.globalData.iconImgUrl,
    backImg: app.globalData.iconImgUrl+"arrow-left.svg",
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    backIcon: true,
    title: "揭榜详情",
    upImgVisible: true,
    formeImg: [],
    modalVisible: false,
    content: '',
    teamNum: '',
    markName: '',
    mark: 0
  },
  // 查看大图
  lookBigPic(e) {
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: '', // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  // 获取详情信息
  getDetail(id) {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + 'reward/xsDetails.do',
      method: 'post',
      data: {
        userCode: wx.getStorageSync('userCode'),
        id: id
      },
      success(res) {
        const details = res.data.details
        console.log(res)
        if (details) {
          const imgArr = []
          if (details.img1) {
            imgArr.push(details.img1)
          }
          if (details.img2) {
            imgArr.push(details.img2)
          }
          if (details.img3) {
            imgArr.push(details.img3)
          }
          that.setData({
            content: details.content,
            teamNum: details.teamNum,
            markName: details.markName,
            mark: details.mark,
            formeImg: imgArr,
            remainNum: details.remainNum,
            markType: details.markType,
            join: details.join,
            doing: details.doing
          })
        }
      }
    })
  },
  // 图片错误
  loadImgErr(e) {
    console.log(e)
  },
  // 我要揭榜
  announce() {
    var id = this.data.id
    var join = this.data.join,
      that = this
    if (!join) {
      wx.request({
        url: app.globalData.baseUrl + 'reward/join.do',
        method: 'post',
        data: {
          id: id,
          userCode: wx.getStorageSync('userCode')
        },
        success(res) {
          if (res.data.rstype) {
            wx.showToast({
              title: '已揭榜成功',
            })
            that.setData({
              join: true,
              doing: true
            })
          } else {
            wx.showToast({
              title: res.data.rsmsg,
              image: '../../../assets/images/icon/error.png'
            })
          }
        }
      })
    }
  },
  // 点击工作已完成
  finishedWork() {
    var id = this.data.id
    var join = this.data.join,
      that = this
    if (join) {
      wx.request({
        url: app.globalData.baseUrl + 'reward/doOver.do',
        method: 'post',
        data: {
          id: id,
          userCode: wx.getStorageSync('userCode')
        },
        success(res) {
          if (res) {
            wx.showToast({
              title: '已完成工作',
            })
            that.setData({
              doing: false
            })
          } else {
            wx.showToast({
              title: '网络错误',
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '请先揭榜',
        image: '../../../assets/images/icon/error.png'
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log((options.id))
    this.getDetail(options.id)
    this.setData({
      id: options.id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})